* Clean up 

clear all

/*
* Set working directory: please set your own
cd "/Users/vicentevalentim/Desktop/fragmentation_replication_bjps"
*/

********************************************************************************
******************* Manipulations on the files with findings *******************
********************************************************************************

use Results/analyses.dta, clear

gen outcome_type = " "

gen optimal = 0
replace optimal = 1 if id == "No. parties just above" & Predictor == "ENPP" & FE == "None" & Bandwidth == 50 & Sample == "Whole"
replace optimal = 1 if Model == "OLS" & Predictor == "ENPP" & FE == "None" & Sample == "Whole"

replace id = "Treatm status of closest parties" if id == "Instrumenting treatm status of closest parties"
replace id = "No. parties just above" if id == "Means comparison"
gen inst = Instruments


drop if Sample == "Polity score above 6"

tostring inst, replace
replace inst = " " if inst == "99"
replace inst = "One instrument" if inst == "1"
replace inst = "Two instruments" if inst == "2"
replace inst = "Three instruments" if inst == "3"
replace inst = "Four instruments" if inst == "4"

replace outcome_type = "Summary\nmeasures" if Outcome == "comp_all" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_polyarchy" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_libdem" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_partipdem" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_delibdem" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_egaldem" 
replace outcome_type = "Summary\nmeasures" if Outcome == "polity2" 
replace outcome_type = "Summary\nmeasures" if Outcome == "comp_summary" 

replace outcome_type = "Other\noutcomes" if Outcome == "turnout" | Outcome == "public_goods" | ///
Outcome == "comp_other"

replace  outcome_type = "Repr underpriviliged\ngroups" if ///
 Outcome == "power_distributed_sexorient" |  Outcome == "pwr_distributd_sexorient" |  Outcome == "pwr_distributd_sexorient_10" | ///
 Outcome == "repr_disadvantaged_groups" |  Outcome == "repr_disadvantaged_groups_5" |  Outcome == "repr_disadv_groups_10" | /// 
 Outcome == "repr_disadv_groups" | Outcome == "power_distr_socioeco_pos_5" | Outcome == "power_distr_socioeco_pos_10" | ///
 Outcome == "power_distr_socgroup" | Outcome == "power_distr_socgroup_5" | Outcome == "power_distr_socgroup_10" | ///
 Outcome == "comp_underpriviliged"
 
replace  outcome_type = "Electoral irreg/violence" if ///
 Outcome == "other_elec_violence" |  Outcome == "other_election_irreg" |  Outcome == "elec_gov_intimid" | ///
 Outcome == "electionvote_buy" |  Outcome == "comp_violence_stnd" | ///
 Outcome == "other_elec_violence_5" |  Outcome == "other_election_irreg_5" |  Outcome == "elec_gov_intimid_5" | ///
 Outcome == "electionvote_buy_5" |  Outcome == "comp_violence_5_stnd" | ///
 Outcome == "other_elec_violence_10" |  Outcome == "other_election_irreg_10" |  Outcome == "elec_gov_intimid_10" | ///
 Outcome == "electionvote_buy_10" |  Outcome == "comp_violence_10_stnd"
 
replace  outcome_type = "Descriptive\nrepr of women"  if ///
 Outcome == "power_distributed_by_gender" |  Outcome == "power_distributed_by_gndr" | ///
 Outcome == "election_women_cabinet" |  Outcome == "election_women_cabinet_5" | Outcome == "election_women_cabinet_10" | ///
 Outcome == "percent_female_mps" | | Outcome == "percent_female_mps_5" | Outcome == "percent_female_mps_10" | ///
 Outcome == "comp_womenrep"

 replace  outcome_type = "Accountability" ///
if Outcome == "accountability_index" |  Outcome == "vertical_acc_index" |  Outcome == "diagonal_acc_index" | ///
 Outcome == "horizontal_acc_index" | Outcome == "comp_accountability"
 
 replace  outcome_type = "Gov instability" ///
if Outcome == "coalition" |  Outcome == "gov_fract_index"  | Outcome == "closeness_nextel" | ///
Outcome == "comp_governability"

replace  outcome_type = "Corruption" ///
if Outcome == "public_sector_corruption" |  Outcome == "public_sector_corruption_5" |  Outcome == "public_sector_corruption_10" | ///
 Outcome == "public_sector_theft" |  Outcome == "public_sector_theft_5" |  Outcome == "public_sector_theft_10" | ///
 Outcome == "exec_embezzlement_theft" |  Outcome == "exec_embezzlement_theft_5" |  Outcome == "exec_embezzlement_theft_10" | ///
 Outcome == "exec_bribery_corruption" |  Outcome == "exec_bribery_corruption_5" |  Outcome == "exec_bribery_corruption_10"  | ///
 Outcome == "legislature_corruption" |  Outcome == "legislature_corruption_5" |  Outcome == "legislature_corruption_10" | ///
 Outcome == "comp_corruption" 

* Dropping models used just to start the datasets
drop if Outcome == "fake"

* Fixing outcome names
replace Outcome = "Electoral Dem index (Vdem)" if Outcome == "v2x_polyarchy" 
replace Outcome = "Liberal Dem index (Vdem)" if Outcome == "v2x_libdem" 
replace Outcome = "Participatory Dem index (Vdem)" if Outcome == "v2x_partipdem" 
replace Outcome = "Deliberative Dem index (Vdem)" if Outcome == "v2x_delibdem" 
replace Outcome = "Egalitarian Dem index (Vdem)" if Outcome == "v2x_egaldem" 
replace Outcome = "Polity score" if Outcome == "polity2" 
replace Outcome = "Summary\nmeasures (PCA)" if Outcome == "comp_summary" 

replace Outcome = "Turnout" if Outcome == "turnout" 
replace Outcome = "Power distr by sexual orientation" if Outcome == "pwr_distributd_sexorient" 
replace Outcome = "Power distr by sexual orientation" if Outcome == "power_distributed_sexorient" 
replace Outcome = "Representation disadvantaged groups" if Outcome == "repr_disadv_groups" 
replace Outcome = "Representation disadvantaged groups" if Outcome == "repr_disadvantaged_groups" 
replace Outcome = "Power distr by socioeco position" if Outcome == "power_distr_socioeco_pos" 
replace Outcome = "Power distributed by gender" if Outcome == "power_distributed_by_gender" 
replace Outcome = "Power distributed by gender" if Outcome == "power_distributed_by_gndr" 
replace Outcome = "Power distributed by gender" if Outcome == "power_distributd_by_gndr" 
replace Outcome = "Distance next election" if Outcome == "closeness_nextel" 
replace Outcome = "Accountability index" if Outcome == "accountability_index" 
replace Outcome = "Vertical accountability index" if Outcome == "vertical_acc_index" 
replace Outcome = "Diagonal accountability index" if Outcome == "diagonal_acc_index" 
replace Outcome = "Horizontal accountability index" if Outcome == "horizontal_acc_index" 
replace Outcome = "Coalition government" if Outcome == "coalition" 
replace Outcome = "Index government fractionalization" if Outcome == "gov_fract_index" 
replace Outcome = "Public sector corruption" if Outcome == "public_sector_corruption" 
replace Outcome = "Public sector theft" if Outcome == "public_sector_theft" 
replace Outcome = "Executive embezzlement/theft" if Outcome == "exec_embezzlement_theft" 
replace Outcome = "Political corruption index" if Outcome == "political_corruption_index" 
replace Outcome = "Political corruption index" if Outcome == "pol_corruption_index" 
replace Outcome = "Legislature corruption" if Outcome == "legislature_corruption" 
replace Outcome = "Executive bribery/corruption" if Outcome == "exec_bribery_corruption" 
replace Outcome = "Percent female ministers" if Outcome == "election_women_cabinet"  
replace Outcome = "Percent female MPs" if Outcome == "percent_female_mps"  
replace Outcome = "Power distr by social groups" if Outcome == "power_distr_socgroup"  
replace Outcome = "Provision public goods" if Outcome == "public_goods"  
replace Outcome = "Repr underpriviliged\ngroups (PCA)" if Outcome == "comp_underpriviliged"
replace Outcome = "Other (PCA)" if Outcome == "comp_other"
replace Outcome = "Gov instability (PCA)" if Outcome == "comp_governability"
replace Outcome = "Accountability (PCA)" if Outcome == "comp_accountability"
replace Outcome = "Corruption (PCA)" if Outcome == "comp_corruption"
replace Outcome = "Descriptive repr women (PCA)" if Outcome == "comp_womenrep" 

replace  outcome_type = "Descriptive\nrepr of women"  if Outcome == "Power distributed by gender"
gen pca = 0
replace pca = 1 if Outcome == "Other (PCA)" | Outcome == "Gov instability (PCA)" | ///
 Outcome == "Accountability (PCA)" | Outcome == "Corruption (PCA)" | ///
 Outcome == "Descriptive repr women (PCA)" |  Outcome == "Repr underpriviliged\ngroups (PCA)" | ///
 Outcome == "PCA (all)" | Outcome == "Summary\nmeasures (PCA)"

gen other = 0
replace other = 1 if outcome_type == "Other\noutcomes"

* Renaming coef variable
rename coef Effect

* Saving the file
save Results/analyses_m.dta, replace 

* Making another dataset with just the OLS results
keep if Model == "OLS"

* Saving the file
save Results/analyses_ols.dta,  replace 

* Making another dataset with just the 2SLS results
use Results/analyses_m.dta, clear

drop if Model == "OLS"

* Saving the file
save Results/analyses_2sls.dta, replace 

* Merging with first-stage estimates
* Import the Excel file with first-stage estimates
import excel "Results/firststage.xlsx", sheet("Sheet1") clear

* Rename variable names
rename A Designator
rename B fstat

* Merge
merge 1:1 Designator using Results/analyses_2sls.dta

* Appending the OLS findings
append using Results/analyses_ols.dta

* Dropping fake model
drop if outcome == "fake"

* Save the final dataset
saveold Results/analyses_final.dta, version(12) replace 


*****************************************************
** Same thing on the file only with the Dinas data **
*****************************************************

use Results/analyses_dinasonly.dta, clear

gen outcome_type = " "

replace outcome_type = "Summary\nmeasures" if Outcome == "comp_all" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_polyarchy" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_libdem" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_partipdem" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_delibdem" 
replace outcome_type = "Summary\nmeasures" if Outcome == "v2x_egaldem" 
replace outcome_type = "Summary\nmeasures" if Outcome == "polity2" 
replace outcome_type = "Summary\nmeasures" if Outcome == "comp_summary" 

replace outcome_type = "Other\noutcomes" if Outcome == "turnout" | Outcome == "public_goods" | ///
Outcome == "comp_other"

replace  outcome_type = "Repr underpriviliged\ngroups" if ///
 Outcome == "power_distributed_sexorient" |  Outcome == "pwr_distributd_sexorient" |  Outcome == "pwr_distributd_sexorient_10" | ///
 Outcome == "repr_disadvantaged_groups" |  Outcome == "repr_disadvantaged_groups_5" |  Outcome == "repr_disadv_groups_10" | /// 
 Outcome == "repr_disadv_groups" | Outcome == "power_distr_socioeco_pos_5" | Outcome == "power_distr_socioeco_pos_10" | ///
 Outcome == "power_distr_socgroup" | Outcome == "power_distr_socgroup_5" | Outcome == "power_distr_socgroup_10" | ///
 Outcome == "comp_underpriviliged"
 
replace  outcome_type = "Electoral irreg/violence" if ///
 Outcome == "other_elec_violence" |  Outcome == "other_election_irreg" |  Outcome == "elec_gov_intimid" | ///
 Outcome == "electionvote_buy" |  Outcome == "comp_violence_stnd" | ///
 Outcome == "other_elec_violence_5" |  Outcome == "other_election_irreg_5" |  Outcome == "elec_gov_intimid_5" | ///
 Outcome == "electionvote_buy_5" |  Outcome == "comp_violence_5_stnd" | ///
 Outcome == "other_elec_violence_10" |  Outcome == "other_election_irreg_10" |  Outcome == "elec_gov_intimid_10" | ///
 Outcome == "electionvote_buy_10" |  Outcome == "comp_violence_10_stnd"
 
replace  outcome_type = "Descriptive\nrepr of women"  if ///
 Outcome == "power_distributed_by_gender" |  Outcome == "power_distributed_by_gndr" | ///
 Outcome == "election_women_cabinet" |  Outcome == "election_women_cabinet_5" | Outcome == "election_women_cabinet_10" | ///
 Outcome == "percent_female_mps" | | Outcome == "percent_female_mps_5" | Outcome == "percent_female_mps_10" | ///
 Outcome == "comp_womenrep"

 replace  outcome_type = "Accountability" ///
if Outcome == "accountability_index" |  Outcome == "vertical_acc_index" |  Outcome == "diagonal_acc_index" | ///
 Outcome == "horizontal_acc_index" | Outcome == "comp_accountability"
 
 replace  outcome_type = "Gov instability" ///
if Outcome == "coalition" |  Outcome == "gov_fract_index"  | Outcome == "closeness_nextel" | ///
Outcome == "comp_governability"

replace  outcome_type = "Corruption" ///
if Outcome == "public_sector_corruption" |  Outcome == "public_sector_corruption_5" |  Outcome == "public_sector_corruption_10" | ///
 Outcome == "public_sector_theft" |  Outcome == "public_sector_theft_5" |  Outcome == "public_sector_theft_10" | ///
 Outcome == "exec_embezzlement_theft" |  Outcome == "exec_embezzlement_theft_5" |  Outcome == "exec_embezzlement_theft_10" | ///
 Outcome == "exec_bribery_corruption" |  Outcome == "exec_bribery_corruption_5" |  Outcome == "exec_bribery_corruption_10"  | ///
 Outcome == "legislature_corruption" |  Outcome == "legislature_corruption_5" |  Outcome == "legislature_corruption_10" | ///
 Outcome == "comp_corruption" 
* Dropping outcomes we don't use
drop if Outcome == "comp_all"

* Dropping models used just to start the datasets
drop if Outcome == "fake"

* Fixing outcome names
replace Outcome = "Electoral Dem index (Vdem)" if Outcome == "v2x_polyarchy" 
replace Outcome = "Liberal Dem index (Vdem)" if Outcome == "v2x_libdem" 
replace Outcome = "Participatory Dem index (Vdem)" if Outcome == "v2x_partipdem" 
replace Outcome = "Deliberative Dem index (Vdem)" if Outcome == "v2x_delibdem" 
replace Outcome = "Egalitarian Dem index (Vdem)" if Outcome == "v2x_egaldem" 
replace Outcome = "Polity score" if Outcome == "polity2" 
replace Outcome = "Summary\nmeasures (PCA)" if Outcome == "comp_summary" 

replace Outcome = "Turnout" if Outcome == "turnout" 
replace Outcome = "Power distr by sexual orientation" if Outcome == "pwr_distributd_sexorient" 
replace Outcome = "Power distr by sexual orientation" if Outcome == "power_distributed_sexorient" 
replace Outcome = "Representation disadvantaged groups" if Outcome == "repr_disadv_groups" 
replace Outcome = "Representation disadvantaged groups" if Outcome == "repr_disadvantaged_groups" 
replace Outcome = "Power distr by socioeco position" if Outcome == "power_distr_socioeco_pos" 
replace Outcome = "Power distributed by gender" if Outcome == "power_distributed_by_gender" 
replace Outcome = "Power distributed by gender" if Outcome == "power_distributed_by_gndr" 
replace Outcome = "Power distributed by gender" if Outcome == "power_distributd_by_gndr" 
replace Outcome = "Distance next election" if Outcome == "closeness_nextel" 
replace Outcome = "Accountability index" if Outcome == "accountability_index" 
replace Outcome = "Vertical accountability index" if Outcome == "vertical_acc_index" 
replace Outcome = "Diagonal accountability index" if Outcome == "diagonal_acc_index" 
replace Outcome = "Horizontal accountability index" if Outcome == "horizontal_acc_index" 
replace Outcome = "Coalition government" if Outcome == "coalition" 
replace Outcome = "Index government fractionalization" if Outcome == "gov_fract_index" 
replace Outcome = "Public sector corruption" if Outcome == "public_sector_corruption" 
replace Outcome = "Public sector theft" if Outcome == "public_sector_theft" 
replace Outcome = "Executive embezzlement/theft" if Outcome == "exec_embezzlement_theft" 
replace Outcome = "Political corruption index" if Outcome == "political_corruption_index" 
replace Outcome = "Political corruption index" if Outcome == "pol_corruption_index" 
replace Outcome = "Legislature corruption" if Outcome == "legislature_corruption" 
replace Outcome = "Executive bribery/corruption" if Outcome == "exec_bribery_corruption" 
replace Outcome = "Percent female ministers" if Outcome == "election_women_cabinet"  
replace Outcome = "Percent female MPs" if Outcome == "percent_female_mps"  
replace Outcome = "Power distr by social groups" if Outcome == "power_distr_socgroup"  
replace Outcome = "Provision public goods" if Outcome == "public_goods"  
replace Outcome = "Repr underpriviliged\ngroups (PCA)" if Outcome == "comp_underpriviliged"
replace Outcome = "Other (PCA)" if Outcome == "comp_other"
replace Outcome = "Gov instability (PCA)" if Outcome == "comp_governability"
replace Outcome = "Accountability (PCA)" if Outcome == "comp_accountability"
replace Outcome = "Corruption (PCA)" if Outcome == "comp_corruption"
replace Outcome = "Descriptive repr women (PCA)" if Outcome == "comp_womenrep" 

replace  outcome_type = "Descriptive\nrepr of women"  if Outcome == "Power distributed by gender"
gen pca = 0
replace pca = 1 if Outcome == "Other (PCA)" | Outcome == "Gov instability (PCA)" | ///
 Outcome == "Accountability (PCA)" | Outcome == "Corruption (PCA)" | ///
 Outcome == "Descriptive repr women (PCA)" |  Outcome == "Repr underpriviliged\ngroups (PCA)" | ///
 Outcome == "PCA (all)" | Outcome == "Summary\nmeasures (PCA)"

gen other = 0
replace other = 1 if outcome_type == "Other\noutcomes"

* Renaming coef variable
rename coef Effect

saveold Results/analyses_final_dinasonly.dta, version(12) replace 
